package com.dhcc.core.modules.system.controller;


import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.group.IGroupCache;
import com.dhcc.core.modules.system.entity.Group;
import com.dhcc.core.modules.system.service.IGroupService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.CacheClear;



/**
 * 群组
 * @ClassName: GroupController
 * @Description: TODO
 * @author lifan
 */
@Controller
@RequestMapping("${sys.backendPath}/group")
public class GroupController extends BaseController {

	private String PREFIX = "/backend/system/group/";

	@Autowired
	private IGroupService groupService;
	

    /**
     * 跳转到群组列表首页
     */
    @RequestMapping("/{ispublic}")
    public String index(@PathVariable String ispublic, Model model) {
    	System.out.println(ispublic);
    	if(ispublic.equals("public")){
    		model.addAttribute("ispublic",1);
    	}else {
    		model.addAttribute("ispublic",0);
    	}
        return PREFIX + "group.html";
    }
	
    /**
     * 跳转到添加群组
     */
    @RequestMapping("/to_add/{ispublic}")
    public String groupAdd(@PathVariable int ispublic, Model model) {
    	model.addAttribute("ispublic",ispublic);
        return PREFIX + "group_add.html";
    }
	
	/**
     * 跳转到修改群组
     */
    @RequestMapping("/to_update/{groupId}")
    public String groupUpdate(@PathVariable Long groupId, Model model) {
        Group group = this.groupService.selectById(groupId);
        model.addAttribute("group",group);
        return PREFIX + "group_edit.html";
    }

    
    /**
     * 获取群组的tree列表
     */
    @RequestMapping(value = "/groupTree/{params}")
    @ResponseBody
    public List<ZTreeNode> groupTree(@PathVariable String params) {
    	System.out.println(params);
        List<ZTreeNode> tree = this.groupService.groupTree(params);
        List<ZTreeNode> trees = new ArrayList<ZTreeNode>();
        trees.addAll(tree);
        return trees;
    }

    /**
     * 新增群组
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增群组")
    @CacheClear(IGroupCache.CACHE_NAME)
    public Object add(Group group) {
        if (CommonUtil.isOneEmpty(group)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        group.insert();
        return R.ok();
    }

    /**
     * 删除群组
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除群组")
    @CacheClear(IGroupCache.CACHE_NAME)
    public Object delete(@RequestParam Long groupId) {
        this.groupService.deleteById(groupId,groupId);
        return R.ok();
    }

    /**
     * 修改群组
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改群组")
    @CacheClear(IGroupCache.CACHE_NAME)
    public Object update(Group group) {
        if (CommonUtil.isOneEmpty(group)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        Group old = this.groupService.selectById(group.getId());
        old.setIspublic(group.getIspublic());
        old.setName(group.getName());
        old.setDescription(group.getDescription());
        old.setWeight(group.getWeight());
        old.updateById();
        return R.ok();
    }
	
}
